package com.bjbn.controller;

import com.bjbn.entity.EqptMapping;
import com.bjbn.service.EqptMappingCacheService;
import com.bjbn.util.EqptMappingCacheUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资产映射缓存管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/cache/eqpt-mapping")
@Tag(name = "资产映射缓存管理", description = "资产映射缓存的管理和查询接口")
public class EqptMappingCacheController {
    
    @Autowired
    private EqptMappingCacheService eqptMappingCacheService;
    
    /**
     * 获取缓存统计信息
     */
    @Operation(summary = "获取缓存统计信息", description = "获取资产映射缓存的统计信息，包括命中率、总数量等")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/stats")
    public Map<String, Object> getCacheStats() {
        Map<String, Object> stats = EqptMappingCacheUtil.getCacheStats();
        if (stats == null) {
            stats = new HashMap<>();
            stats.put("error", "缓存服务未初始化");
        }
        return stats;
    }
    
    /**
     * 手动刷新缓存
     */
    @Operation(summary = "手动刷新缓存", description = "手动触发资产映射缓存的刷新操作")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "刷新成功"),
            @ApiResponse(responseCode = "500", description = "刷新失败")
    })
    @PostMapping("/refresh")
    public Map<String, Object> refreshCache() {
        Map<String, Object> result = new HashMap<>();
        try {
            eqptMappingCacheService.refreshCache();
            result.put("success", true);
            result.put("message", "缓存刷新成功");
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            log.error("手动刷新缓存失败", e);
            result.put("success", false);
            result.put("message", "缓存刷新失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据设备编码查询资产映射
     */
    @Operation(summary = "根据设备编码查询资产映射", description = "根据设备编码查询对应的资产映射信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到对应的资产映射"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/by-eqpt-code/{eqptCode}")
    public Map<String, Object> getByEqptCode(
            @Parameter(description = "设备编码", required = true, example = "EQPT001")
            @PathVariable String eqptCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            EqptMapping mapping = EqptMappingCacheUtil.getByEqptCode(eqptCode);
            if (mapping != null) {
                result.put("success", true);
                result.put("data", mapping);
            } else {
                result.put("success", false);
                result.put("message", "未找到对应的资产映射");
            }
        } catch (Exception e) {
            log.error("根据设备编码查询资产映射失败", e);
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据BMS编码查询资产映射
     */
    @Operation(summary = "根据BMS编码查询资产映射", description = "根据BMS编码查询对应的资产映射信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到对应的资产映射"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/by-bms-code/{bmsCode}")
    public Map<String, Object> getByBmsCode(
            @Parameter(description = "BMS编码", required = true, example = "BMS001")
            @PathVariable String bmsCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            EqptMapping mapping = EqptMappingCacheUtil.getByBmsCode(bmsCode);
            if (mapping != null) {
                result.put("success", true);
                result.put("data", mapping);
            } else {
                result.put("success", false);
                result.put("message", "未找到对应的资产映射");
            }
        } catch (Exception e) {
            log.error("根据BMS编码查询资产映射失败", e);
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据资产编码查询资产映射
     */
    @Operation(summary = "根据资产编码查询资产映射", description = "根据资产编码查询对应的资产映射信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到对应的资产映射"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/by-asset-code/{assetCode}")
    public Map<String, Object> getByAssetCode(
            @Parameter(description = "资产编码", required = true, example = "ASSET001")
            @PathVariable String assetCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            EqptMapping mapping = EqptMappingCacheUtil.getByAssetCode(assetCode);
            if (mapping != null) {
                result.put("success", true);
                result.put("data", mapping);
            } else {
                result.put("success", false);
                result.put("message", "未找到对应的资产映射");
            }
        } catch (Exception e) {
            log.error("根据资产编码查询资产映射失败", e);
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据BA编码查询资产映射
     */
    @Operation(summary = "根据BA编码查询资产映射", description = "根据BA编码查询对应的资产映射信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到对应的资产映射"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/by-ba-code/{baCode}")
    public Map<String, Object> getByBaCode(
            @Parameter(description = "BA编码", required = true, example = "BA001")
            @PathVariable String baCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            EqptMapping mapping = EqptMappingCacheUtil.getByBaCode(baCode);
            if (mapping != null) {
                result.put("success", true);
                result.put("data", mapping);
            } else {
                result.put("success", false);
                result.put("message", "未找到对应的资产映射");
            }
        } catch (Exception e) {
            log.error("根据BA编码查询资产映射失败", e);
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据资产类型查询资产映射列表
     */
    @Operation(summary = "根据资产类型查询资产映射列表", description = "根据资产类型查询所有相关的资产映射信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/by-asset-type/{assetType}")
    public Map<String, Object> getByAssetType(
            @Parameter(description = "资产类型", required = true, example = "空调设备")
            @PathVariable String assetType) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<EqptMapping> mappings = EqptMappingCacheUtil.getByAssetType(assetType);
            result.put("success", true);
            result.put("data", mappings);
            result.put("count", mappings != null ? mappings.size() : 0);
        } catch (Exception e) {
            log.error("根据资产类型查询资产映射失败", e);
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取所有资产映射数据
     */
    @Operation(summary = "获取所有资产映射数据", description = "获取缓存中的所有资产映射数据")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/all")
    public Map<String, Object> getAllMappings() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<EqptMapping> mappings = EqptMappingCacheUtil.getAllMappings();
            result.put("success", true);
            result.put("data", mappings);
            result.put("count", mappings != null ? mappings.size() : 0);
        } catch (Exception e) {
            log.error("获取所有资产映射失败", e);
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 检查缓存状态
     */
    @Operation(summary = "检查缓存状态", description = "检查资产映射缓存的初始化状态和统计信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/status")
    public Map<String, Object> getCacheStatus() {
        Map<String, Object> result = new HashMap<>();
        result.put("initialized", EqptMappingCacheUtil.isCacheInitialized());
        result.put("stats", EqptMappingCacheUtil.getCacheStats());
        return result;
    }
}
